package com.itkaien.javaeebased.reflectproxyprogarm;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static Star createProxy(Star star){
           return (Star) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(),new Class[]{Star.class},new InvocationHandler() {
               @Override
               public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                   System.out.println("代理开始");
                   if ("sing".equals(method.getName())){
                       System.out.println("代理开始唱歌");
                   } else{
                       System.out.println("代理开始跳舞");
                   }
                   Object result = method.invoke(star,args);
                   System.out.println("代理结束");
                   return result;
               }
           });
    }

}
